package com.example.springbootp6spy.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootp6spy.common.result.Result;
import com.example.springbootp6spy.domain.dto.UserRecordAddOrEditDTO;
import com.example.springbootp6spy.domain.dto.UserRecordListDTO;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springbootp6spy.mapper.UserRecordMapper;
import com.example.springbootp6spy.domain.UserRecord;
import com.example.springbootp6spy.service.UserRecordService;

import java.util.List;

/**
 * 用户档案 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2022/10/3
 */
@Service
public class UserRecordServiceImpl extends ServiceImpl<UserRecordMapper, UserRecord> implements UserRecordService {

    @Autowired
    private UserRecordMapper userRecordMapper;

    /**
     * 查询用户档案列表
     *
     * @param userRecordListDTO 用户档案查询对象
     * @return
     */
    @Override
    public Result<?> list(UserRecordListDTO userRecordListDTO) {
        IPage<UserRecord> iPage = new Page<>(userRecordListDTO.getPageNo(), userRecordListDTO.getPageSize());
        List<UserRecord> userRecordList = userRecordMapper.selectByNameLikeAndProfession(iPage, userRecordListDTO.getName(), userRecordListDTO.getProfession());
        iPage.setRecords(userRecordList);
        return Result.success(iPage);
    }

    /**
     * 获取用户档案详情
     *
     * @param id 用户档案id
     * @return
     */
    @Override
    public Result<?> get(Integer id) {
        UserRecord userRecord = userRecordMapper.selectById(id);
        return Result.success(userRecord);
    }

    @Autowired
    private ModelMapper modelMapper;

    /**
     * 添加用户档案
     *
     * @param userRecordAddOrEditDTO 用户档案添加对象
     * @return
     */
    @Override
    public Result<?> add(UserRecordAddOrEditDTO userRecordAddOrEditDTO) {
        UserRecord userRecord = modelMapper.map(userRecordAddOrEditDTO, UserRecord.class);
        int insert = userRecordMapper.insert(userRecord);
        return insert > 0 ? Result.success() : Result.failed();
    }

    /**
     * 用户档案id
     *
     * @param id                     用户档案id
     * @param userRecordAddOrEditDTO 用户档案修改对象
     * @return
     */
    @Override
    public Result<?> edit(Integer id, UserRecordAddOrEditDTO userRecordAddOrEditDTO) {
        UserRecord userRecord = modelMapper.map(userRecordAddOrEditDTO, UserRecord.class);
        userRecord.setId(id);
        int update = userRecordMapper.updateById(userRecord);
        return update > 0 ? Result.success() : Result.failed();
    }

    /**
     * 删除用户档案
     *
     * @param id 用户档案id
     * @return
     */
    @Override
    public Result<?> delete(Integer id) {
        int delete = userRecordMapper.deleteById(id);
        return delete > 0 ? Result.success() : Result.failed();
    }
}
